package com.senven.api.controller;

import com.senven.api.annotation.LogInfo;
import com.senven.api.commom.ResponseCode;
import com.senven.api.commom.ResponsePage;
import com.senven.api.commom.ResponseResult;
import com.senven.api.dao.GoodsMapper;
import com.senven.api.exception.GlobalException;
import com.senven.api.pojo.Goods;
import com.senven.api.service.GoodsService;

import com.senven.api.utils.ReponseResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.xml.transform.Result;
import java.util.List;

/**
 * @author cckimjs
 */
@RequestMapping("/api/organ/goods")
@Controller
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    //@LogInfo(operateInfo = "查询社团物资列表")
    @GetMapping("/queryGoodsList")
    @ResponseBody
    public ResponsePage<Goods>  getGoods(@RequestParam(value = "current", defaultValue = "1") int current,
                                         @RequestParam(value = "size",defaultValue = "10")int size){
        return goodsService.queryGoodsList(current,size);
    }


    /**
     * OID查询社团物资信息
     * 路径中获取参数
     * @param oid
     * @return
     */
    @GetMapping(value = "/queryGoodsListByOid")
    @ResponseBody
    public Goods queryGoodsListByOid(@RequestParam("oid") int oid){
        return goodsService.queryGoodsListByOid(oid);
    }


    /**
     * 根据物资名进行查询
     * @param name
     * @return
     */
    @GetMapping(value = "/queryGoodsListByName")
    @ResponseBody
    public ResponsePage<Goods> queryGoodsListByName(@RequestParam("name") String name,
                                                    @RequestParam(value = "current", defaultValue = "1") int current,
                                                    @RequestParam(value = "size",defaultValue = "10")int size){
        return goodsService.queryGoodsListByName(name,current,size);
    }


    /**
     * 插入一个物资
     * @param goods
     */
    @RequestMapping(value = "/insertGoods",method = RequestMethod.POST)
    @ResponseBody
    public ResponseResult insertGoods(@RequestBody Goods goods){
        int result;
        try {
            result = goodsService.insertGoods(goods);
        } catch (Exception e) {
            throw new GlobalException(ResponseCode.ERROR.getCode(), "数据库错误");
        }

        if (result == 0){
            return ReponseResultUtil.error(-1,"物资插入失败");
        }else{
            return ReponseResultUtil.success("物资插入成功");
        }
    }

    /**
     * 更新一个物资
     * @param goods
     */
    @RequestMapping(value = "/UpdateGoods",method = RequestMethod.POST)
    @ResponseBody
    public ResponseResult updateGoods(@RequestBody Goods goods){
        //System.out.println(goods.getName());
        try {
            int result = goodsService.updateGoods(goods);
            if (result == 0) {
                return ReponseResultUtil.error(-1, "物资更新失败");
            } else {
                return ReponseResultUtil.success("物资更新成功");
            }
        }catch (Exception e){
            throw new GlobalException(ResponseCode.ERROR.getCode(),"数据库错误");
        }
    }

    /**
     * 删除一个物资
     * @param oid
     */
    @GetMapping("/deleteGoods")
    @ResponseBody
    public ResponseResult deleteGoods(@RequestParam("id") int oid){
        //System.out.println(organ.getName());
        try {
            int result = goodsService.deleteGoods(oid);
            if (result == 0) {
                return ReponseResultUtil.error(-1, "物资删除失败");
            } else {
                return ReponseResultUtil.success("物资删除成功");
            }
        }catch (Exception e){
            throw new GlobalException(ResponseCode.ERROR.getCode(),"数据库错误");
        }
    }

}